package com.windoent.yugioh.ui.activity.saishi;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.CreateGameStackUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/24 15:15
 * desc   :窗前比赛
 */
public class CreateGamesStepTwoActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private LinearLayout mLlShuoming;
    private Button mBtnGetCode;
    private EditText mEtPrize;
    private EditText mEtRemark;
    private String countyid;
    private String cityid;
    private String winning;
    private String zone_id;
    private String rounds;
    private String rule;
    private String address_position;
    private String condition;
    private String type;
    private String cost;
    private String address;
    private String players;
    private String start_at;
    private String contact;
    private String name;


    @Override
    public int getLayout() {
        return R.layout.activity_create_games_step_two;
    }

    @Override
    public void initView() {
        super.initView();

        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mLlShuoming = (LinearLayout) findViewById(R.id.ll_shuoming);
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);
        mEtPrize = (EditText) findViewById(R.id.et_prize);
        mEtRemark = (EditText) findViewById(R.id.et_remark);
    }

    @Override
    public void initData() {
        super.initData();
        CreateGameStackUtils.getInstance().addActivity(this);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        contact = intent.getStringExtra("contact");
        start_at = intent.getStringExtra("start_at");
        players = intent.getStringExtra("players");
        address = intent.getStringExtra("address");
        cost = intent.getStringExtra("cost");
        type = intent.getStringExtra("type");
        condition = intent.getStringExtra("condition");
        address_position = intent.getStringExtra("address_position");
        rule = intent.getStringExtra("rule");
        rounds = intent.getStringExtra("rounds");
        winning = intent.getStringExtra("winning");
        zone_id = intent.getStringExtra("zone_id");
        cityid = intent.getStringExtra("cityid");
        countyid = intent.getStringExtra("countyid");
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mLlShuoming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //说明
                startActivity(new Intent(mContext, CreateGamesShuoMingActivity.class));
            }
        });
        mBtnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String prize = mEtPrize.getText().toString().trim();
                String remark = mEtRemark.getText().toString().trim();
                boolean notEmpty = notEmpty(prize);
                if (notEmpty) {
                    shangjiaCreateGameTwo(prize, remark);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    //创建比赛
    private void shangjiaCreateGameTwo(String prize, String remark) {
        showLoadingDialog("");
        PhoneLive.shangjiaCreateGameTwo(getUserToken(), prize, remark, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean json = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(json.getMsg());
                            //下一步
                            Intent intent = new Intent(mContext, CreateGamesStepThreeActivity.class);
                            intent.putExtra("name", name);
                            intent.putExtra("contact", contact);
                            intent.putExtra("start_at", start_at);
                            intent.putExtra("players", players);
                            intent.putExtra("address", address);
                            intent.putExtra("cost", cost);
                            intent.putExtra("type", type);
                            intent.putExtra("condition", condition);
                            intent.putExtra("address_position", address_position);
                            intent.putExtra("rule", rule);
                            intent.putExtra("rounds", rounds);
                            intent.putExtra("winning", winning);
                            intent.putExtra("zone_id", zone_id);
                            intent.putExtra("cityid", cityid);
                            intent.putExtra("countyid", countyid);
                            intent.putExtra("prize", prize);
                            intent.putExtra("remark", remark);
                            startActivity(intent);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    private boolean notEmpty(String prize) {
        if (BaseMethod.isNull(prize)) {
            ToastUtils.showText(getString(R.string.string_input_prize_null));
            return false;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "赛事创建比赛第二步界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "赛事创建比赛第二步界面");
    }
}
